const app = getApp()
const cookieUtil = require("../../utils/manager_cookie.js")

Page({
  data: {
    clubName: "",
    actiTitle: "",
    startTime: '',
    endTime: '',
    actiContent: "",
    arrIndex: 0,
  },

  onLoad(options) {
    var that = this
    that.setData({
      clubName: options.managerclubName,
    })

    my.request({
			url: 'https://www.zjsu.club/club/map/',
			method: 'GET',
			success: (res) => {
				var data = res.data.data;
        var objectArray = [];
				console.log(data)
				for (var i = 0; i < data.length; i++) {
					var info = {
						id: data[i].locationId,
						name: data[i].name,
					};
					objectArray.push(info);
				}
				this.setData({
					objectArray,
				})
			},
			fail: (err) => {
				console.log('error', err);
			},
		});
  },

  onTitleBlur(e) {
    //console.log(e)
    this.setData({
      actiTitle: e.detail.value
    })
  },

  onTitleComfirm(e) {
    //console.log(e)
    this.setData({
      actiTitle: e.detail.value
    })
  },

  onContentBlur(e) {
    //console.log(e)
    this.setData({
      actiContent: e.detail.value
    })
  },

  onContentComfirm(e) {
    //console.log(e)
    this.setData({
      actiContent: e.detail.value
    })
  },

  beginDatePickerYMDHMS() {
    my.datePicker({
      format: 'yyyy-MM-dd HH:mm:ss',
      currentDate: '2020-04-01 00:00:00',
      startDate: '2020-04-01 00:00:00',
      endDate: '2020-09-01 00:00:00',
      success: (res) => {
        console.log(res.date)
        /*my.alert({
          content: '您选择的日期为: ' + res.date
        });*/
        this.setData({
          startTime: res.date
        })
      },
    });
  },

  endDatePickerYMDHMS() {
    my.datePicker({
      format: 'yyyy-MM-dd HH:mm:ss',
      currentDate: '2020-04-01 00:00:00',
      startDate: '2020-04-01 00:00:00',
      endDate: '2020-09-01 00:00:00',
      success: (res) => {
        console.log(res.date)
        /*my.alert({
          content: '您选择的日期为: ' + res.date
        });*/
        this.setData({
          endTime: res.date
        })
      },
    });
  },

  bindObjPickerChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      arrIndex: e.detail.value,
      locationName: this.data.objectArray[e.detail.value].name,
      locationId: this.data.objectArray[e.detail.value].id,
    });
  },

  onSubmit(e) {
    //clubName,actiTitle,locationName,locationId,startTime,endTime,actiContent
    let data = {
      clubName: this.data.clubName,
      actiTitle: this.data.actiTitle,
      locationName: this.data.locationName,
      locationId: this.data.locationId,
      startTime: this.data.startTime,
      endTime: this.data.endTime,
      actiContent: this.data.actiContent
    }

    const url = app.globalData.baseUrl + app.globalData.clubUrl

    let promise = new Promise((resolve, reject) => {
			my.request({
				url,
				method: "POST",
				data,
				headers: {
					'content-type': 'application/x-www-form-urlencoded'
				},
				success: (res) => {
					resolve(res)
				}
			})
		})

    promise.then((res) => {
			console.log(res)
			if (res.data.code == "fail") {
				my.showToast({
					type: 'fail',
					content: res.data.message,
					duration: 2000,
				})
			} else {
				//保存cookie
				const cookie = cookieUtil.getSessionIdFromResponse(res)
				cookieUtil.setCookieToStorage(cookie)

				my.showToast({
					type: 'success',
					content: res.data.message,
					duration: 2000,
					success: () => {
						my.switchTab({
							url: '/pages/clubmanager/clubmanager'
						})
					}
				})
			}
		})
  }
});